package com.seabook.happy.website.daos;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Text;
import com.seabook.happy.website.pojos.BlogArticle;

public class BlogArticleDao {
	private DatastoreService datastore = DatastoreServiceFactory
			.getDatastoreService();

	public void saveArticle(BlogArticle article) {
		Entity entity = new Entity(EntitesKind.BLOG_ARTICLE_KIND);
		entity.setProperty(BlogArticle.TITLE, article.getTitle());
		entity.setProperty(BlogArticle.CREATE_DATE, article.getCreatedDate());
		entity.setProperty(BlogArticle.MODIFIED_DATE, article.getModifiedDate());
		entity.setProperty(BlogArticle.CONTENT, article.getContent());
		entity.setProperty(BlogArticle.CATEGORY, article.getCategory());
		entity.setProperty(BlogArticle.THUMBS_UP, article.getThumbsUp());
		entity.setProperty(BlogArticle.THUMBS_DOWN, article.getThumbsDown());
		datastore.put(entity);
	}
	
	public BlogArticle findBlogArticle(String title) {
		Query query = new Query(EntitesKind.BLOG_ARTICLE_KIND);
		query.addFilter(BlogArticle.TITLE, FilterOperator.EQUAL, title);
		Entity article = datastore.prepare(query).asSingleEntity();
		return transferEntity2Article(article);
	}
	
	public BlogArticle findBlogArticle(Key key) {
		Entity article = null;
		try {
			article = datastore.get(key);
		} catch (EntityNotFoundException e) {
			e.printStackTrace();
		}
		return transferEntity2Article(article);
	}

	private BlogArticle transferEntity2Article(Entity entity) {
		BlogArticle article = new BlogArticle();
		article.setKey(entity.getKey());
		article.setTitle((String) entity.getProperty(BlogArticle.TITLE));
		article.setContent((Text) entity.getProperty(BlogArticle.CONTENT));
		article.setCreatedDate((Date) entity
				.getProperty(BlogArticle.CREATE_DATE));
		article.setModifiedDate((Date) entity
				.getProperty(BlogArticle.MODIFIED_DATE));
		article.setThumbsUp((Long) entity.getProperty(BlogArticle.THUMBS_UP));
		article.setThumbsDown((Long) entity
				.getProperty(BlogArticle.THUMBS_DOWN));
		return article;
	}
	
	private List<BlogArticle> transferEntity2Article(List<Entity> entities) {
		List<BlogArticle> blogArticles = new ArrayList<BlogArticle>();
		
		for (Entity entity : entities) {
			blogArticles.add(transferEntity2Article(entity));
		}
		
		return blogArticles;
	}
	
	public List<BlogArticle> listLatestBlogArticles(int number) {
		Query query = new Query(EntitesKind.BLOG_ARTICLE_KIND);
		query.addSort(BlogArticle.CREATE_DATE, Query.SortDirection.DESCENDING);
		List<Entity> blogEntities = datastore.prepare(query).asList(
				FetchOptions.Builder.withLimit(number));
		
		return transferEntity2Article(blogEntities);
	}

}
